/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.SQLException;
import java.util.Vector;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.swing.JOptionPane;
import model.Uf;
import util.Conexao;

/**
 *
 * @author Ewerton
 */
public class UFDAO extends GenericDao<Uf> {
    Conexao c = new Conexao();
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("Controle_de_EstoquePU");
    EntityManager manager = factory.createEntityManager();

    public void salvar(Uf uf){
        gravar(uf);
    }

    public void alterar(Uf uf){
        atualizar(uf);
    }

    public void excluir(Uf uf){
        Uf u = listar(uf);
        remover(u);
    }

    public Uf listar(Uf uf){
        return getEntityManager().find(Uf.class, uf.getIdUf());
    }
    
    public Vector<Uf> listaTodos() {
      c.conecta();
      Vector<Uf> resultados = new Vector<Uf>();
      try {
         c.executeSQL("SELECT * FROM uf ORDER BY sigla");
         while (c.resultset.next()) {
            Uf temp = new Uf();
            temp.setDescricao(c.resultset.getString("SIGLA"));
            resultados.add(temp);
         }
         return resultados;
      } catch (SQLException e) {
         JOptionPane.showMessageDialog(null, e);
         return null;
      }
   }
    
   public Uf buscarPorSigla(String nome){
        Query query = manager.createQuery("SELECT u FROM Uf u WHERE u.sigla = '"+nome+"'");
        Uf u = (Uf) query.getSingleResult();
        if(u == null){
            JOptionPane.showMessageDialog(null, "UF não encontrado");
            return null;
        }
        else{
            return u;
        }
    }
}
